//获取sessionStorage
export const ss_getItem = function (key) {
  let data = sessionStorage.getItem(key);
  if (data) {
    try {
      data = JSON.parse(data);
      if (typeof data === 'object') {
        return data
      } else {
        return data.toString();
      }
    } catch (e) {
      return data.toString();
    }
  }
  return null
};
//设置sessionStorage
export const ss_setItem = function (key, value) {
  if (typeof value === 'object') {
    value = JSON.stringify(value);
  }
  sessionStorage.setItem(key, value);
  return value
};
//获取localStorage
export const ls_getItem = function (key) {
  let data = localStorage.getItem(key);
  if (data) {
    try {
      data = JSON.parse(data);
      if (typeof data === 'object') {
        return data
      } else {
        return data.toString();
      }
    } catch (e) {
      return data.toString();
    }
  }
  return null
};
//设置localStorage
export const ls_setItem = function (key, value) {
  if (typeof value === 'object') {
    value = JSON.stringify(value);
  }
  localStorage.setItem(key, value);
  return value
};

//路由跳转
import router from '@/router/index.js';

export const toRouter = function (name = '', isReplace = 0, params = {}, isQuery = 0) {
  let path_name = name.indexOf('/') > -1 ? {path: name} : {name};
  if (isReplace) {
    if (isQuery) {
      router.replace({...path_name, query: params}).then(r => null)
    } else {
      router.replace({...path_name, params}).then(r => null)
    }
  } else {
    if (isQuery) {
      router.push({...path_name, query: params}).then(r => null)
    } else {
      router.push({...path_name, params}).then(r => null)
    }
  }
};
//根据数值判断污染程度
export const paramIsOverproof = function (param, value, ap) {
  const getLevelInfo = function (level, ap) {
    if (level === 0) {
      return {
        color: "#C2C3CC",
        text: "离线",
        level: 0,
        fillStyle: "rgba(194,195,204, " + ap + " )"
      };
    } else if (level === 1) {
      return {
        color: "#68CB00",
        text: "优质",
        level: 1,
        fillStyle: "rgba(104,203,0, " + ap + " )"
      };
    } else if (level === 2) {
      return {
        color: "#F7E300",
        text: "良好",
        level: 2,
        fillStyle: "rgba(247,227,0, " + ap + " )"
      };
    } else if (level === 3) {
      return {
        color: "#FB890E",
        text: "轻度",
        level: 3,
        fillStyle: "rgba(251,137,14, " + ap + " )"
      };
    } else if (level === 4) {
      return {
        color: "#DF2C00",
        text: "中度",
        level: 4,
        fillStyle: "rgba(223,44,0, " + ap + " )"
      };
    } else if (level === 5) {
      return {
        color: "#B514BB",
        text: "重度",
        level: 5,
        fillStyle: "rgba(181,20,187, " + ap + " )"
      };
    } else if (level === 6) {
      return {
        color: "#6E0474",
        text: "严重",
        level: 6,
        fillStyle: "rgba(110,4,116, " + ap + " )"
      };
    }
  };
  let allParamLevel = {
    aqi: [0, 50, 100, 150, 200, 300],
    pm10: [0, 50, 150, 250, 350, 420],
    "pm2.5": [0, 35, 75, 115, 150, 250],
    pm25: [0, 35, 75, 115, 150, 250],
    so2: [0, 150, 500, 650, 800],
    co: [0, 5, 10, 35, 60, 90],
    no2: [0, 100, 200, 700, 1200, 2340],
    o3: [0, 160, 200, 300, 400, 800],
    tsp: [0, 50, 150, 250, 350, 420],
    h2s: [0, 50, 150, 250, 350, 420],
    nh3: [0, 50, 150, 250, 350, 420]
  };
  value = Number(value);
  let len = allParamLevel[param] ? allParamLevel[param].length : 0;
  for (let i = 0; i < len; i++) {
    if (value === 0) return getLevelInfo(0, ap);
    let row = allParamLevel[param][i];
    if (value <= row) {
      return getLevelInfo(i, ap);
    }
  }
  return getLevelInfo(len, ap);
};

//根据污染等级返回背静颜色
export const getPollutionLevel = function (pollutionLevel) {
  var color = "";
  if (!pollutionLevel) {
    color = "#bbbbbb";
  } else if (pollutionLevel == 1) {
    color = "#4bb86e";
  } else if (pollutionLevel == 2) {
    color = "#EDED11";
  } else if (pollutionLevel == 3) {
    color = "#FF7E00";
  } else if (pollutionLevel == 4) {
    color = "#f00";
  } else if (pollutionLevel == 5) {
    color = "#99004C";
  } else if (pollutionLevel == 6) {
    color = "#7E0023";
  }
  return color;
};

export const GPSToMap = {
  PI: 3.14159265358979324,
  x_pi: (3.14159265358979324 * 3000.0) / 180.0,
  delta: function (lat, lon) {
    // Krasovsky 1940
    //
    // a = 6378245.0, 1/f = 298.3
    // b = a * (1 - f)
    // ee = (a^2 - b^2) / a^2;
    var a = 6378245.0; //  a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
    var ee = 0.00669342162296594323; //  ee: 椭球的偏心率。
    var dLat = this.transformLat(lon - 105.0, lat - 35.0);
    var dLon = this.transformLon(lon - 105.0, lat - 35.0);
    var radLat = (lat / 180.0) * this.PI;
    var magic = Math.sin(radLat);
    magic = 1 - ee * magic * magic;
    var sqrtMagic = Math.sqrt(magic);
    dLat = (dLat * 180.0) / (((a * (1 - ee)) / (magic * sqrtMagic)) * this.PI);
    dLon = (dLon * 180.0) / ((a / sqrtMagic) * Math.cos(radLat) * this.PI);
    return {lat: dLat, lng: dLon};
  },

  //WGS-84 to GCJ-02
  gcj_encrypt: function (wgsLon, wgsLat) {
    if (this.outOfChina(wgsLat, wgsLon)) return {lat: wgsLat, lng: wgsLon};

    var d = this.delta(wgsLat, wgsLon);
    return {lat: wgsLat + d.lat, lng: wgsLon + d.lat};
  },
  //GCJ-02 to WGS-84
  gcj_decrypt: function (gcjLon, gcjLat) {
    if (this.outOfChina(gcjLat, gcjLon)) return {lat: gcjLat, lng: gcjLon};

    var d = this.delta(gcjLat, gcjLon);
    return {lat: gcjLat - d.lat, lng: gcjLon - d.lat};
  },
  //GCJ-02 to WGS-84 exactly
  gcj_decrypt_exact: function (gcjLon, gcjLat) {
    var initDelta = 0.01;
    var threshold = 0.000000001;
    var dLat = initDelta,
      dLon = initDelta;
    var mLat = gcjLat - dLat,
      mLon = gcjLon - dLon;
    var pLat = gcjLat + dLat,
      pLon = gcjLon + dLon;
    var wgsLat,
      wgsLon,
      i = 0;
    while (1) {
      wgsLat = (mLat + pLat) / 2;
      wgsLon = (mLon + pLon) / 2;
      var tmp = this.gcj_encrypt(wgsLat, wgsLon);
      dLat = tmp.lat - gcjLat;
      dLon = tmp.lon - gcjLon;
      if (Math.abs(dLat) < threshold && Math.abs(dLon) < threshold) break;

      if (dLat > 0) pLat = wgsLat;
      else mLat = wgsLat;
      if (dLon > 0) pLon = wgsLon;
      else mLon = wgsLon;

      if (++i > 10000) break;
    }
    //console.log(i);
    return {lat: wgsLat, lng: wgsLon};
  },
  //GCJ-02 to BD-09
  bd_encrypt: function (gcjLon, gcjLat) {
    let x = gcjLon,
      y = gcjLat;
    let z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * this.x_pi);
    let theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * this.x_pi);
    let bdLon = z * Math.cos(theta) + 0.0065;
    let bdLat = z * Math.sin(theta) + 0.006;
    return {lat: bdLat, lng: bdLon};
  },
  //BD-09 to GCJ-02
  bd_decrypt: function (bdLon, bdLat) {
    var x = bdLon - 0.0065,
      y = bdLat - 0.006;
    var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * this.x_pi);
    var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * this.x_pi);
    var gcjLon = z * Math.cos(theta);
    var gcjLat = z * Math.sin(theta);
    return {lat: gcjLat, lng: gcjLon};
  },
  //WGS-84 to Web mercator
  //mercatorLat -> y mercatorLon -> x
  mercator_encrypt: function (wgsLon, wgsLat) {
    var x = (wgsLon * 20037508.34) / 180;
    var y =
      Math.log(Math.tan(((90 + wgsLat) * this.PI) / 360)) / (this.PI / 180);
    y = (y * 20037508.34) / 180;
    return {lat: y, lng: x};
    /*
    if ((Math.abs(wgsLon) > 180 || Math.abs(wgsLat) > 90))
        return null;
    var x = 6378137.0 * wgsLon * 0.017453292519943295;
    var a = wgsLat * 0.017453292519943295;
    var y = 3189068.5 * Math.log((1.0 + Math.sin(a)) / (1.0 - Math.sin(a)));
    return {'lat' : y, 'lon' : x};
    //*/
  },
  // Web mercator to WGS-84
  // mercatorLat -> y mercatorLon -> x
  mercator_decrypt: function (mercatorLon, mercatorLat) {
    var x = (mercatorLon / 20037508.34) * 180;
    var y = (mercatorLat / 20037508.34) * 180;
    y =
      (180 / this.PI) *
      (2 * Math.atan(Math.exp((y * this.PI) / 180)) - this.PI / 2);
    return {lat: y, lng: x};
    /*
    if (Math.abs(mercatorLon) < 180 && Math.abs(mercatorLat) < 90)
        return null;
    if ((Math.abs(mercatorLon) > 20037508.3427892) || (Math.abs(mercatorLat) > 20037508.3427892))
        return null;
    var a = mercatorLon / 6378137.0 * 57.295779513082323;
    var x = a - (Math.floor(((a + 180.0) / 360.0)) * 360.0);
    var y = (1.5707963267948966 - (2.0 * Math.atan(Math.exp((-1.0 * mercatorLat) / 6378137.0)))) * 57.295779513082323;
    return {'lat' : y, 'lon' : x};
    //*/
  },
  // two point's distance
  distance: function (lonA, latA, lonB, latB) {
    let earthR = 6371000;
    let x =
      Math.cos((latA * this.PI) / 180) *
      Math.cos((latB * this.PI) / 180) *
      Math.cos(((lonA - lonB) * this.PI) / 180);
    let y = Math.sin((latA * this.PI) / 180) * Math.sin((latB * this.PI) / 180);
    let s = x + y;
    if (s > 1) s = 1;
    if (s < -1) s = -1;
    let alpha = Math.acos(s);
    let distance = alpha * earthR;
    return distance;
  },
  outOfChina: function (lon, lat) {
    if (lon < 72.004 || lon > 137.8347) return true;
    if (lat < 0.8293 || lat > 55.8271) return true;
    return false;
  },
  transformLat: function (x, y) {
    var ret =
      -100.0 +
      2.0 * x +
      3.0 * y +
      0.2 * y * y +
      0.1 * x * y +
      0.2 * Math.sqrt(Math.abs(x));
    ret +=
      ((20.0 * Math.sin(6.0 * x * this.PI) +
        20.0 * Math.sin(2.0 * x * this.PI)) *
        2.0) /
      3.0;
    ret +=
      ((20.0 * Math.sin(y * this.PI) + 40.0 * Math.sin((y / 3.0) * this.PI)) *
        2.0) /
      3.0;
    ret +=
      ((160.0 * Math.sin((y / 12.0) * this.PI) +
        320 * Math.sin((y * this.PI) / 30.0)) *
        2.0) /
      3.0;
    return ret;
  },
  transformLon: function (x, y) {
    var ret =
      300.0 +
      x +
      2.0 * y +
      0.1 * x * x +
      0.1 * x * y +
      0.1 * Math.sqrt(Math.abs(x));
    ret +=
      ((20.0 * Math.sin(6.0 * x * this.PI) +
        20.0 * Math.sin(2.0 * x * this.PI)) *
        2.0) /
      3.0;
    ret +=
      ((20.0 * Math.sin(x * this.PI) + 40.0 * Math.sin((x / 3.0) * this.PI)) *
        2.0) /
      3.0;
    ret +=
      ((150.0 * Math.sin((x / 12.0) * this.PI) +
        300.0 * Math.sin((x / 30.0) * this.PI)) *
        2.0) /
      3.0;
    return ret;
  }
};


/**
 * 根据一个经纬度及距离角度，算出另外一个经纬度
 * @param {*} lng 经度 113.3960698
 * @param {*} lat 纬度 22.941386
 * @param {*} brng 方位角 45 ---- 正北方：000°或360° 正东方：090° 正南方：180° 正西方：270°
 * @param {*} dist 90000距离(米)
 *
 */
export const mapNumberUtil = {
  rad(d) {
    return (d * Math.PI) / 180.0;
  },
  deg(x) {
    return (x * 180) / Math.PI;
  },
  getLonAndLat(lng, lat, brng, dist) {
    //大地坐标系资料WGS-84 长半径a=6378137 短半径b=6356752.3142 扁率f=1/298.2572236
    let a = 6378137;
    let b = 6356752.3142;
    let f = 1 / 298.257223563;

    let lon1 = lng * 1;
    let lat1 = lat * 1;
    let s = dist;
    let alpha1 = mapNumberUtil.rad(brng);
    let sinAlpha1 = Math.sin(alpha1);
    let cosAlpha1 = Math.cos(alpha1);

    let tanU1 = (1 - f) * Math.tan(mapNumberUtil.rad(lat1));
    let cosU1 = 1 / Math.sqrt(1 + tanU1 * tanU1),
      sinU1 = tanU1 * cosU1;
    let sigma1 = Math.atan2(tanU1, cosAlpha1);
    let sinAlpha = cosU1 * sinAlpha1;
    let cosSqAlpha = 1 - sinAlpha * sinAlpha;
    let uSq = (cosSqAlpha * (a * a - b * b)) / (b * b);
    let A = 1 + (uSq / 16384) * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
    let B = (uSq / 1024) * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));

    let sigma = s / (b * A),
      sigmaP = 2 * Math.PI;
    while (Math.abs(sigma - sigmaP) > 1e-12) {
      var cos2SigmaM = Math.cos(2 * sigma1 + sigma);
      var sinSigma = Math.sin(sigma);
      var cosSigma = Math.cos(sigma);
      let deltaSigma =
        B *
        sinSigma *
        (cos2SigmaM +
          (B / 4) *
          (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM) -
            (B / 6) *
            cos2SigmaM *
            (-3 + 4 * sinSigma * sinSigma) *
            (-3 + 4 * cos2SigmaM * cos2SigmaM)));
      sigmaP = sigma;
      sigma = s / (b * A) + deltaSigma;
    }

    let tmp = sinU1 * sinSigma - cosU1 * cosSigma * cosAlpha1;
    let lat2 = Math.atan2(
      sinU1 * cosSigma + cosU1 * sinSigma * cosAlpha1,
      (1 - f) * Math.sqrt(sinAlpha * sinAlpha + tmp * tmp)
    );
    let lambda = Math.atan2(
      sinSigma * sinAlpha1,
      cosU1 * cosSigma - sinU1 * sinSigma * cosAlpha1
    );
    let C = (f / 16) * cosSqAlpha * (4 + f * (4 - 3 * cosSqAlpha));
    let L =
      lambda -
      (1 - C) *
      f *
      sinAlpha *
      (sigma +
        C *
        sinSigma *
        (cos2SigmaM + C * cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM)));

    let revAz = Math.atan2(sinAlpha, -tmp); // final bearing

    return {
      lng: (lon1 + mapNumberUtil.deg(L)).toFixed(6),
      lat: mapNumberUtil.deg(lat2).toFixed(6)
    };
  },
  //根据两点经纬度坐标计算出经纬度之间的夹角；
  getAngle(lng_a, lat_a, lng_b, lat_b) {
    let a = ((90 - lat_b) * Math.PI) / 180;
    let b = ((90 - lat_a) * Math.PI) / 180;
    let AOC_BOC = ((lng_b - lng_a) * Math.PI) / 180;
    let cosc =
      Math.cos(a) * Math.cos(b) + Math.sin(a) * Math.sin(b) * Math.cos(AOC_BOC);
    let sinc = Math.sqrt(1 - cosc * cosc);
    let sinA = (Math.sin(a) * Math.sin(AOC_BOC)) / sinc;
    let A = (Math.asin(sinA) * 180) / Math.PI;
    let res = 0;
    if (lng_b > lng_a && lat_b > lat_a) res = A;
    else if (lng_b > lng_a && lat_b < lat_a) res = 180 - A;
    else if (lng_b < lng_a && lat_b < lat_a) res = 180 - A;
    else if (lng_b < lng_a && lat_b > lat_a) res = 360 + A;
    else if (lng_b > lng_a && lat_b == lat_a) res = 90;
    else if (lng_b < lng_a && lat_b == lat_a) res = 270;
    else if (lng_b == lng_a && lat_b > lat_a) res = 0;
    else if (lng_b == lng_a && lat_b < lat_a) res = 180;
    return res;
  }

};






